import React, { useState } from "react";
import { Modal, Checkbox, message } from "antd";
import { CopyOutlined } from "@ant-design/icons";
import type { Task } from "../../../types/project";
import { mockProjectApi } from "../../../api/mockProjectApi";

interface CopyTaskModalProps {
  /** 弹窗是否可见 */
  visible: boolean;
  /** 取消回调 */
  onCancel: () => void;
  /** 要复制的任务 */
  task: Task;
  /** 项目ID */
  projectId: string;
}

/**
 * 复制任务弹窗组件
 * 允许用户选择要复制的任务内容部分
 */
const CopyTaskModal: React.FC<CopyTaskModalProps> = ({ visible, onCancel, task, projectId }) => {
  const [loading, setLoading] = useState(false);
  const [copyOptions, setCopyOptions] = useState({
    title: true,
    description: true,
    assignee: false,
    dueDate: false,
    subTasks: true,
    cards: true,
    tags: true
  });

  /**
   * 处理复制选项变更
   * @param field 字段名
   * @param checked 是否选中
   */
  const handleOptionChange = (field: string, checked: boolean) => {
    setCopyOptions(prev => ({
      ...prev,
      [field]: checked
    }));
  };

  /**
   * 全选/全不选
   * @param checked 是否全选
   */
  const handleSelectAll = (checked: boolean) => {
    setCopyOptions({
      title: checked,
      description: checked,
      assignee: checked,
      dueDate: checked,
      subTasks: checked,
      cards: checked,
      tags: checked
    });
  };

  /**
   * 执行复制操作
   */
  const handleCopy = async () => {
    setLoading(true);
    try {
      await mockProjectApi.copyTask(projectId, task.id, copyOptions);
      message.success("任务复制成功");
      onCancel();
      // 这里可以触发数据刷新
      window.location.reload(); // 简单的刷新方式，实际项目中应该使用更优雅的方式
    } catch (error) {
      console.error("复制任务失败:", error);
      message.error("复制任务失败，请重试");
    } finally {
      setLoading(false);
    }
  };

  /**
   * 检查是否至少选择了一个选项
   */
  const hasSelection = Object.values(copyOptions).some(Boolean);

  /**
   * 检查是否全选
   */
  const isAllSelected = Object.values(copyOptions).every(Boolean);

  /**
   * 检查是否部分选中
   */
  const isIndeterminate = hasSelection && !isAllSelected;

  return (
    <Modal
      title={
        <div className="flex items-center gap-2">
          <CopyOutlined />
          <span>复制任务</span>
        </div>
      }
      open={visible}
      onCancel={onCancel}
      onOk={handleCopy}
      confirmLoading={loading}
      okText="确认复制"
      cancelText="取消"
      okButtonProps={{ disabled: !hasSelection }}
    >
      <div className="space-y-4">
        <div className="bg-gray-50 p-3 rounded-lg">
          <div className="font-medium text-gray-900 mb-1">原任务信息</div>
          <div className="text-sm text-gray-600">
            <div>标题: {task.title}</div>
            <div>描述: {task.description || "无"}</div>
            <div>负责人: {task.assignee?.name || "未分配"}</div>
            <div>子任务: {task.subTasks.length} 个</div>
            <div>卡片: {task.cards.length} 个</div>
            <div>标签: {task.tags?.length || 0} 个</div>
          </div>
        </div>

        <div>
          <div className="flex items-center justify-between mb-3">
            <span className="font-medium text-gray-900">选择要复制的内容:</span>
            <Checkbox
              checked={isAllSelected}
              indeterminate={isIndeterminate}
              onChange={e => handleSelectAll(e.target.checked)}
            >
              全选
            </Checkbox>
          </div>

          <div className="space-y-3">
            {/* 基本信息 */}
            <div>
              <div className="font-medium text-gray-700 mb-2">基本信息</div>
              <div className="space-y-2 ml-4">
                <Checkbox checked={copyOptions.title} onChange={e => handleOptionChange("title", e.target.checked)}>
                  任务标题
                  <span className="text-gray-500 ml-2">(复制后会添加"(副本)"后缀)</span>
                </Checkbox>

                <Checkbox
                  checked={copyOptions.description}
                  onChange={e => handleOptionChange("description", e.target.checked)}
                  disabled={!task.description}
                >
                  任务描述
                  {!task.description && <span className="text-gray-400 ml-2">(原任务无描述)</span>}
                </Checkbox>

                <Checkbox
                  checked={copyOptions.tags}
                  onChange={e => handleOptionChange("tags", e.target.checked)}
                  disabled={!task.tags || task.tags.length === 0}
                >
                  标签
                  {(!task.tags || task.tags.length === 0) && <span className="text-gray-400 ml-2">(原任务无标签)</span>}
                </Checkbox>
              </div>
            </div>

            {/* 分配信息 */}
            <div>
              <div className="font-medium text-gray-700 mb-2">分配信息</div>
              <div className="space-y-2 ml-4">
                <Checkbox
                  checked={copyOptions.assignee}
                  onChange={e => handleOptionChange("assignee", e.target.checked)}
                  disabled={!task.assignee}
                >
                  负责人
                  {!task.assignee && <span className="text-gray-400 ml-2">(原任务未分配负责人)</span>}
                </Checkbox>

                <Checkbox
                  checked={copyOptions.dueDate}
                  onChange={e => handleOptionChange("dueDate", e.target.checked)}
                  disabled={!task.dueDate}
                >
                  截止时间
                  {!task.dueDate && <span className="text-gray-400 ml-2">(原任务无截止时间)</span>}
                </Checkbox>
              </div>
            </div>

            {/* 详细内容 */}
            <div>
              <div className="font-medium text-gray-700 mb-2">详细内容</div>
              <div className="space-y-2 ml-4">
                <Checkbox
                  checked={copyOptions.subTasks}
                  onChange={e => handleOptionChange("subTasks", e.target.checked)}
                  disabled={task.subTasks.length === 0}
                >
                  子任务 ({task.subTasks.length} 个)
                  <div className="text-xs text-gray-500 ml-2">复制的子任务状态将重置为未完成</div>
                </Checkbox>

                <Checkbox
                  checked={copyOptions.cards}
                  onChange={e => handleOptionChange("cards", e.target.checked)}
                  disabled={task.cards.length === 0}
                >
                  卡片 ({task.cards.length} 个)
                </Checkbox>
              </div>
            </div>
          </div>
        </div>

        <div className="bg-blue-50 p-3 rounded-lg">
          <div className="text-sm text-blue-800">
            <div className="font-medium mb-1">复制说明:</div>
            <ul className="list-disc list-inside space-y-1 text-xs">
              <li>复制的任务状态将设置为"未开始"</li>
              <li>复制的任务将放置在原任务所在的分组中</li>
              <li>子任务的完成状态将重置为未完成</li>
              <li>任务的创建时间和更新时间将重新生成</li>
            </ul>
          </div>
        </div>
      </div>
    </Modal>
  );
};

export default CopyTaskModal;
